﻿Public Class FormStore
    Private Sub LoadData()
        Cmd.Parameters("@s") = "%" & Me.txtSearch.Text & "%"
        Me.DataGridView1.DataSource = Cmd.ExecuteDataTable("select * from Store where upper(StoreName) like upper(@s) and Active=1")
    End Sub

    Private Sub FoemStore_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        UI.FormatDataGrid(Me.DataGridView1)
        LoadData()
    End Sub

    Private Sub txtSearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSearch.TextChanged
        LoadData()
    End Sub

    Private Sub btnDelet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelet.Click
        If DataGridView1.SelectedRows.Count = 0 Then Exit Sub

        If MessageBox.Show("Are you sure want to delete this Item?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.OK Then
            Exit Sub
        End If
        Dim row = Me.DataGridView1.SelectedRows(0)
        Dim id = row.Cells("StoreId").Value
        Cmd.Parameters("id") = id
        Cmd.ExecuteNonQuery("update Store set Active=0 where StoreId=@id")
        LoadData()
    End Sub

    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        If Me.DataGridView1.SelectedRows.Count = 0 Then
            MsgBox("Pleas select row first")
            Exit Sub
        End If

        Dim row = Me.DataGridView1.SelectedRows(0)
        Dim frm As New FormStoreEditor()
        frm.txtId.Text = row.Cells("StoreId").Value
        frm.txtName.Text = row.Cells("StoreName").Value
        If frm.ShowDialog() = Windows.Forms.DialogResult.OK Then
            LoadData()
        End If
    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        FormStoreEditor.ShowDialog()
    End Sub
End Class